Radios
Radios allow users to choose only one item from a list of mutually exclusive choices.
#Examples
Radios
are always used in groups, and each option is represented by one Radios.Radio
in the group.
Default state:
one option is selected in a radio group by default. If you click on another option, the current option will be deselected.Disabled state:
indicates that an option exists but is not available in a particular case. The disabled state can be used to indicate that an option may be available again at a later time. If possible, provide hint text or a visual clue to explain why the option is disabled to avoid user confusion.Label with dynamic text:
pay attention to how the labels change in size and how the visual elements surrounding them are affected.Label with icon or tooltip:
help the user understand the context of the option.
const [radioValue, setRadioValue] = useState("option-1");
return (
<Radios
// Radios must be labeled via aria if no visible label is provided (i.e. via FormElementWrapper)
aria-label="An example option group"
value={radioValue}
onChange={setRadioValue}
>
<Radios.Radio value="option-1">Regular option</Radios.Radio>
<Radios.Radio value="option-2" disabled>
Disabled option
</Radios.Radio>
<Radios.Radio value="option-3">
<Icon>
<IconApprove />
</Icon>
<InlineText>Option with left icon</InlineText>
</Radios.Radio>
<Radios.Radio value="option-4">
{(props: { checked: boolean; disabled: boolean }) => (
<>
<InlineText
emphasis={props.checked ? "medium" : "normal"}
lineHeight="multi-line"
tone={props.disabled ? "subtle" : undefined}
>
Option with right icon and dynamic text style
</InlineText>
<Icon>
<IconApprove />
</Icon>
</>
)}
</Radios.Radio>
<Tooltip variant={{ type: "interactive" }} content="Extra info on the option 5">
<Radios.Radio value="option-5">Option with tooltip</Radios.Radio>
</Tooltip>
</Radios>
);
#Properties
Property | Description | Defined | Value |
---|---|---|---|
valueRequired | object Value of the form control | ||
onChangeRequired | function Callback for onChange event | ||
nameOptional | string Name applied to the form control | ||
idOptional | string Id applied to the form control | ||
invalidOptional | boolean Is the form control invalid | ||
onBlurOptional | function Callback for onBlur event | ||
aria-labelOptional | string Label of the form control | ||
aria-describedbyOptional | string ID of an an element that describes what the form control is for | ||
aria-labelledbyOptional | string ID of an an element that labels this form control | ||
childrenOptional | element | ||
directionOptional | "horizontal" | "vertical" | ||
data-observe-keyOptional | string Unique string, used by external script e.g. for event tracking | ||
classNameOptional | string Custom className that's applied to the outermost element (only intended for special cases) | ||
styleOptional | object Style object to apply custom inline styles (only intended for special cases) |
#Guidelines
#Best practices
#Do not use when
#Accessibility
Explore detailed guidelines for this component: Accessibility Specifications
#Writing
#Notable changes
#Migrating from FancyLab 16.x to FancyLab 17.x
The Radios
component on FancyLab 16.x or prior takes an array of control
objects as input/property, which is terse and works well with typechecking, but it plays poorly with composition. As an example, adding a tooltip to a radio label is not possible using this structure as well as changing the icon position to after or before the label.
So, in FancyLab 17.x, the Radios
component was refactored to allow composing other components with them. Now, instead of having a controls object, subcomponents are used to create the controls.
Please find below an example that illustrates the usage before and after these changes. Note that the controls
property has been replaced by children of type Radios.Radio
.
#Before
<Radios
value={radioValue}
onChange={setRadioValue}
controls={[
{
value: "option-1",
label: "Option 1",
},
{
value: "option-2",
label: "Option 2",
},
{
value: "option-3",
label: "Option 3",
},
]}
/>
#After
<Radios value={radioValue} onChange={setRadioValue}>
<Radios.Radio value="option-1">Option 1</Radios.Radio>
<Radios.Radio value="option-2">Option 2</Radios.Radio>
<Radios.Radio value="option-3">Option 3</Radios.Radio>
</Radios>